package Lab7;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class MostOccurrences {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        HashMap<Integer, Integer> occurrences = new HashMap<>();
        int maxCount = 0;

        System.out.println("Enter numbers (0 to end):");

        while (true) {
            int number = scanner.nextInt();
            if (number == 0) {
                break;
            }

            // Update the occurrences count for each number
            int currentCount = occurrences.getOrDefault(number, 0) + 1;
            occurrences.put(number, currentCount);

            // Track the maximum occurrence in a single pass
            if (currentCount > maxCount) {
                maxCount = currentCount;
            }
        }

        // Find all numbers with the maximum occurrence
        System.out.println("Number(s) with the most occurrences:");
        for (Map.Entry<Integer, Integer> entry : occurrences.entrySet()) {
            if (entry.getValue() == maxCount) {
                System.out.println(entry.getKey() + " (occurred " + maxCount + " times)");
            }
        }

        scanner.close();
    }
}
